<?php
/**
 * 微校云车票控制器
 * Author: 孙武 QQ:1228746736
 * Date: 2017/10/28
 * Time: 20:17
 */


namespace Home\Controller;

use Home\Model\IndexModel;
use Home\Model\TicketModel;
use Think\Controller;

class TicketController extends HomeController
{

    private $_M;


    public function __construct()
    {
        parent::__construct();
        $this->_M = new TicketModel('addr');
    }

    /**
     * 车票查询界面v1.0.0基本完善
     */
    public function ticketQueryResult($from = '', $to = "", $time = '')
    {


        session('from', $from);
        session('to', $to);
        session('time', $time);

        if (session('from') && session('to') && session('time')) {
            $from = session('from');
            $to = session('to');
            $time = session('time');
        }

        /*检查是否满足查询条件*/
        if (!$this->checkAddrQueryInfo($from, $to, $time)) {
            $this->err('系统数据处理错误！查询车票信息缺失');
            exit;
        }


        $addrs = $this->_M->getAddrsInfoByFromAndTo($from, $to);//车票信息
//        dump($addrs);
//        exit;

        $doneTicketInfo = array();//处理好的发车信息
        foreach ($addrs as $v) {
            $dbTime = date('Y-m-d', $v['addr_depart_time']);//2017-10-31

            /*根据时间过滤数据 */
            if ($dbTime == $time) {

                $v['addr_depart_time'] = getFormateDateFromTimestamp($v['addr_depart_time']);

                if ($v['addr_depart_condition'] == '1') {//必发车
                    $ticketId = $v['addr_id'];
                    $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));//剩余位置

                    if ($needNum < 0) {
                        $v['title'] = "必发车(满座)";
                    } else {
                        $v['title'] = "必发车(余票$needNum)";
                    }
                } else {//预发车


                    $ticketId = $v['addr_id'];
                    $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));//剩余位置


                    if ($needNum > 0) {
                        $v['title'] = "预发车(余票" . $needNum . ")";
                    } else {
                        $v['title'] = "预发车(满座)";//满人处理
                    }
                }
                $doneTicketInfo[] = $v;
            }

        }

        if (C('HOME_DEBUG')) {
            dump($doneTicketInfo);
        }

        $this->assign("title", "车票查询结果");

        $this->assign("from", $from);
        $this->assign("to", $to);
        $this->assign("time", $time);
        $this->assign("tickets", $doneTicketInfo);
        $this->assign("ticketNum", count($doneTicketInfo));
        $this->display();
    }


    /**
     * 车票查询界面v1.0.0基本完善 备份
     */
    public function ticketQueryResultBak()
    {


        $from = $_POST['from'];
        $to = $_POST['to'];
        $time = $_POST['time'];
        //dump($time);//2017-7-16

        //cookie 保存数据
        cookie('from', $from);
        cookie('to', $to);
        cookie('time', $time);

        //条件验证
//        if (!$this->checkAddrQueryInfo()) {
//            $this->err('系统数据处理错误！查询车票信息缺失');
//            exit;
//        }

        if (!(empty($from) && empty($to))) {
            //选择出发地和目的地


            //车票信息
            $addrs = $this->_M->getAddrsInfoByFromAndTo($from, $to);

            //处理好的发车信息
            $doneTicketInfo = array();
            foreach ($addrs as $v) {
                $dbTime = date('Y-m-d', $v['addr_depart_time']);
                // var_dump($date);//2017-10-31

                /*根据时间过滤数据 */
                if ($dbTime = $time) {
                    $v['addr_depart_time'] = getFormateDateFromTimestamp($v['addr_depart_time']);

                    //处理必发已发车
                    if ($v['addr_depart_condition'] == '1') {

                        $ticketId = $v['addr_id'];
                        $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));
                        if ($needNum < 0) {
                            $v['title'] = "必发车(满座)";
                        } else {
                            $v['title'] = "必发车(余票$needNum)";
                        }
                    } else {


                        //还需再约的人数
                        $ticketId = $v['addr_id'];
                        $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));

                        //满人处理
                        if ($needNum > 0) {
                            $v['title'] = "预发车(余票" . $needNum . ")";
                        } else {
                            $v['title'] = "预发车(满座)";
                        }
                    }
                    $doneTicketInfo[] = $v;
                }

            }
        } else {

            //没有选择出发地和目的地
            $addrs = $this->_M->where(array('addr_status' => 1))->select();
            foreach ($addrs as $v) {
                $dbTime = date('Y-m-d', $v['addr_depart_time']);
                // var_dump($date);//2017-10-31

                /*根据时间过滤数据 */

                $v['addr_depart_time'] = getFormateDateFromTimestamp($v['addr_depart_time']);

                //处理必发已发车
                if ($v['addr_depart_condition'] == '1') {

                    $ticketId = $v['addr_id'];
                    $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));
                    if ($needNum < 0) {
                        $v['title'] = "必发车(满座)";
                    } else {
                        $v['title'] = "必发车(余票$needNum)";
                    }
                } else {


                    //还需再约的人数
                    $ticketId = $v['addr_id'];
                    $needNum = ($v['addr_depart_person_num']) - ($this->getNeedNum($ticketId));

                    //满人处理
                    if ($needNum > 0) {
                        $v['title'] = "预发车(余票" . $needNum . ")";
                    } else {
                        $v['title'] = "预发车(满座)";
                    }
                }
                $doneTicketInfo[] = $v;
            }
//            dump($doneTicketInfo);
//            exit;
        }

        if (C('HOME_DEBUG')) {
            dump($doneTicketInfo);
        }

        $this->assign("title", "车票查询结果");

        $this->assign("from", $from);
        $this->assign("to", $to);
        $this->assign("time", $time);
        $this->assign("tickets", $doneTicketInfo);
        $this->assign("ticketNum", count($doneTicketInfo));
        $this->display();
    }


    /**
     * 检查车票查询信息是否完整
     * @return bool 完整:true  不完整:false
     */
    private function checkAddrQueryInfo($from, $to, $time)
    {
        if (empty($from) || empty($to) || empty($time)) {
            return false;
        }
        return true;
    }


    /**
     * 获取车票的所需数目(商家填)
     * 比如说需要32人才能发车
     * @param $ticketId
     * @return mixed
     */
    private function getNeedNum($ticketId)
    {
        return $this->_M->getNeedNum($ticketId);

    }


}